home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / swtools / mipsABI / examples / sup / filecopy.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  2.5 KB  |  66 lines

  1. /*
  2.  * Copyright (c) 1991 Carnegie Mellon University
  3.  * All Rights Reserved.
  4.  * 
  5.  * Permission to use, copy, modify and distribute this software and its
  6.  * documentation is hereby granted, provided that both the copyright
  7.  * notice and this permission notice appear in all copies of the
  8.  * software, derivative works or modified versions, and any portions
  9.  * thereof, and that both notices appear in supporting documentation.
  10.  *
  11.  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  12.  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  13.  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  14.  *
  15.  * Carnegie Mellon requests users of this software to return to
  16.  *
  17.  *  Software Distribution Coordinator   or   Software.Distribution@CS.CMU.EDU
  18.  *  School of Computer Science
  19.  *  Carnegie Mellon University
  20.  *  Pittsburgh PA 15213-3890
  21.  *
  22.  * any improvements or extensions that they make and grant Carnegie the rights
  23.  * to redistribute these changes.
  24.  */
  25. /*  filecopy  --  copy a file from here to there
  26.  *
  27.  *  Usage:  i = filecopy (here,there);
  28.  *    int i, here, there;
  29.  *
  30.  *  Filecopy performs a fast copy of the file "here" to the
  31.  *  file "there".  Here and there are both file descriptors of
  32.  *  open files; here is open for input, and there for output.
  33.  *  Filecopy returns 0 if all is OK; -1 on error.
  34.  *
  35.  *  I have performed some tests for possible improvements to filecopy.
  36.  *  Using a buffer size of 10240 provides about a 1.5 times speedup
  37.  *  over 512 for a file of about 200,000 bytes.  Of course, other
  38.  *  buffer sized should also work; this is a rather arbitrary choice.
  39.  *  I have also tried inserting special startup code to attempt
  40.  *  to align either the input or the output file to lie on a
  41.  *  physical (512-byte) block boundary prior to the big loop,
  42.  *  but this presents only a small (about 5% speedup, so I've
  43.  *  canned that code.  The simple thing seems to be good enough.
  44.  *
  45.  *  HISTORY
  46.  * 20-Nov-79  Steven Shafer (sas) at Carnegie-Mellon University
  47.  *    Rewritten for VAX; same as "filcopy" on PDP-11.  Bigger buffer
  48.  *    size (20 physical blocks) seems to be a big win; aligning things
  49.  *    on block boundaries seems to be a negligible improvement at
  50.  *    considerable cost in complexity.
  51.  *
  52.  */
  53.  
  54. #define BUFFERSIZE 10240
  55.  
  56. int filecopy (here,there)
  57. int here,there;
  58. {
  59.     register int kount;
  60.     char buffer[BUFFERSIZE];
  61.     kount = 0;
  62.     while (kount == 0 && (kount=read(here,buffer,BUFFERSIZE)) > 0)
  63.         kount -= write (there,buffer,kount);
  64.     return (kount ? -1 : 0);
  65. }
  66.